package com.yzt.message.widget.chatinput.widget;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.yzt.message.R;
import com.yzt.plugin_widget.viewPager.ViewPagerAdapter;
import com.yzt.zhuju.richtext.entity.EmojiFatherBean;

import java.util.ArrayList;
import java.util.List;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

/**
 * author : Irene
 * e-mail : 821970751@qq.com
 * date   : 2020/7/30 1:04 PM
 * desc   :
 */
public class CustomEmojiView {

    private ViewPager mViewPager;
    private ViewPagerAdapter mViewPagerAdapter;
    private List<Fragment> mFragments;

    private RecyclerView mEmojiRv;
    private EmojiDialogAdapter mEmojiDialogAdapter;
    private List<EmojiFatherBean> mSelectList;
    private ImageView iv_edit_del;
    private TextView tv_edit_sure;

    public void bindView(FragmentActivity mActivity, View view) {
        bindView(mActivity, view, null);
    }

    public void bindView(FragmentActivity mActivity, View view, Fragment fragment) {
        loadData();
        mEmojiRv = view.findViewById(R.id.emoji_rv);
        mViewPager = view.findViewById(R.id.emoji_vp);
        iv_edit_del = view.findViewById(R.id.iv_edit_del);
        tv_edit_sure = view.findViewById(R.id.tv_edit_sure);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mActivity);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mEmojiRv.setLayoutManager(layoutManager);
        mEmojiDialogAdapter = new EmojiDialogAdapter(mActivity);
        mEmojiRv.setAdapter(mEmojiDialogAdapter);

        iv_edit_del.setVisibility(View.VISIBLE);
        tv_edit_sure.setVisibility(View.GONE);

        AndroidEmoji1.init(view.getContext());
        mEmojiDialogAdapter.initData(mSelectList);
        mEmojiDialogAdapter.setOnClickEmojiSelectListener(new EmojiDialogAdapter.OnClickEmojiSelectListener() {
            @Override
            public void selectIcon(int position) {
                try {
                    mViewPager.setCurrentItem(position);
                    iv_edit_del.setVisibility(View.VISIBLE);
                    tv_edit_sure.setVisibility(View.GONE);
                } catch (Exception ignored) {

                }
            }
        });

        List<String> titleList = new ArrayList<>();
        titleList.add("");
        if (fragment != null) {
            mViewPagerAdapter = new ViewPagerAdapter(fragment.getChildFragmentManager(), mFragments, titleList);
        } else {
            mViewPagerAdapter = new ViewPagerAdapter(mActivity.getSupportFragmentManager(), mFragments, titleList);
        }
        mViewPager.setAdapter(mViewPagerAdapter);
    }

    public void setDefaultSelect() {
        setSelect(0);
    }

    public void setSelect(int position) {
        if (null != mViewPager && null != mSelectList && position >= 0 && position < mSelectList.size()) {
            mViewPager.setCurrentItem(position, false);
        }
    }

    public void loadData() {
        mSelectList = new ArrayList<>();
        mSelectList.add(new EmojiFatherBean());
        mFragments = new ArrayList<>();
        mSelectList.add(new EmojiFatherBean());
        mFragments.add(EmojiItemFragment.newInstance(new ArrayList<>(), 7, 1, listener));
    }

    public void closeEmoji() {
    }

    public OnSelectEmojiListener listener;

    public void setOnSelectEmojiListener(OnSelectEmojiListener listener) {
        this.listener = listener;
    }

    public interface OnSelectEmojiListener {
        void selectEmoji(String msg, String emojiPath, int gameResult, int type);
    }
}
